package com.lizemin.mybatis.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author lzm
 * @date 2025/5/20 22:17
 * @description
 */
public class SqlUtil {

    public static int getPlaceHolderCount(String sql) {
        Pattern pattern = Pattern.compile("#\\{[^}]+}");
        Matcher matcher = pattern.matcher(sql);
        int count = 0;
        // 遍历所有匹配结果
        while (matcher.find()) {
            count++;
        }
        return count;
    }

    public static List<String> getPlaceholders(String sql) {
        List<String> placeholders = new ArrayList<>();
        // 定义匹配 #{...} 形式的正则表达式
        Pattern pattern = Pattern.compile("#\\{([^}]+)}");
        Matcher matcher = pattern.matcher(sql);
        // 遍历所有匹配结果
        while (matcher.find()) {
            // 获取匹配组 1 的内容，即 #{...} 中的内容
            placeholders.add(matcher.group(1));
        }
        return placeholders;
    }

}
